#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python
import numpy as np
import os
from datetime import datetime

class Data:
	def __init__(self,given_name='background',
				columns=1,
				rows=1,
				path="",
				light=True,
				temperature=16,
				scan_mode="field",
				phase=0,#in degree
				modulation_frequency=100,#in kHz
				modulation_amplitude=1,#in mT
				receiver_mode=2,
				receiver_gain=300,
				time_constant=0.3, #in s
				frequency=0,
				power=0,
				field=0,
				picture_saving_format='eps',
				comments="",
				user=''):
		"""Constructor"""
		self.name=given_name
		self.n_columns=columns
		self.n_rows=rows
		self.data=np.zeros((rows,columns))
		self.data_path=path
		self.full_saving_path=""
		self.light_on=light
		self.temperature=temperature
		self.temperature_unit= "K"
		self.scanning_mode=scan_mode
		self.phase=phase
		self.modulation_frequency=modulation_frequency
		self.modulation_amplitude=modulation_amplitude
		self.receiver_mode=receiver_mode
		self.receiver_gain=receiver_gain
		self.time_constant=time_constant
		self.frequency=frequency
		self.frequency_unit="GHz"
		self.RF_power=power
		self.RF_power_unit="mW"
		self.field=field
		self.extension=".csv"
		self.picture_saving_format=picture_saving_format
		self.comments=comments
		self.user=user
		self.date=self.Get_Date()
		self.delimiter=' '
		self.newline='\n'
		
	def Get_Date(self):
		date=datetime.now()
		return str(date.year)+"-"+str(date.month)+"-"+str(date.day)+"_"+str(date.hour)+"-"+str(date.minute)
		
	def Get_Average(self):
		""" """
		return np.average(self.data)
	
	def LinearScale(self,i,f,n_points=0,step=0):
		"""
		To implement
		"""
		if n_points==0:
			pass
		elif step==0:
			pass
	
	def Save(self,form="numpy array"):
		"""docstring for Save"""
		
		if form=="numpy array":
			f=open(self.data_path+self.name,'w')
			f.write(str(self.data))
			f.close()
		else:
			np.savetxt(self.data_path+self.name+".txt",self.data)
			
	def Save_data(self):
		self.full_saving_path=self.data_path+os.sep+self.date+"_"+self.name
		f = open(self.full_saving_path+self.extension, 'w')
		
		header=self.name+": experiment performed by "+self.user+self.newline
		header+=self.date+self.newline
		header+=self.comments+self.newline
		header+="Temperature: "+str(self.temperature)+" "+self.temperature_unit+self.newline
		header+="Scanning: "+self.scanning_mode+self.newline
		header+="Lockin phase: "+str(self.phase)+ " deg"+self.newline
		header+="Lockin modulation frequency: "+str(self.modulation_frequency)+" kHz"+self.newline
		header+="Lockin modulation amplitude: "+str(self.modulation_amplitude)+" mT"+self.newline
		header+="Receiver mode: "+str(self.receiver_mode)+self.newline
		header+="Receiver gain: "+str(self.receiver_gain)+self.newline
		header+="Time constant: "+str(self.time_constant)+" s"+self.newline
		if self.scanning_mode=="frequency":
			header+="Static field= "+str(self.field)+self.newline
		elif self.scanning_mode=="field":
			header+="Static frequency= "+str(self.frequency) + " "+self.frequency_unit+self.newline
			header+="Static frequency power = "+str(self.RF_power) + " "+ self.RF_power_unit+self.newline
		f.write(header)
		for line in range(self.n_rows):
			s=""
			for column in range(self.n_columns):
				s=s+str(self.data[line,column])+self.delimiter
			s=s+self.newline
			f.write(s)
		f.close()
